﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Sunboxsoft.WinForm;
using WebRef = Sunboxsoft.ASMS.Client.Domain.WebRef;
using Service = Sunboxsoft.ASMS.Client.Domain.Service;
using Sunboxsoft.Common;
using Sunboxsoft.ASMS.Client.UI.Properties;
namespace Sunboxsoft.ASMS.Client.UI.Capital
{
    /// <summary>
    /// 资金出帐列表
    /// </summary>
    public partial class CapitalIncreaseBillListView : UserControl, IView
    {
        #region fields
        Promise _promise = new Promise();
        private int _pageIndex;
        private int _pageCount;
        #endregion

        public CapitalIncreaseBillListView()
        {
            InitializeComponent();
            this.GCData.EmbeddedNavigator.ButtonClick += new DevExpress.XtraEditors.NavigatorButtonClickEventHandler(EmbeddedNavigator_ButtonClick);
        }

        #region IView 成员

        public void RegisterAction(ActionNodeCollection collection)
        {
            collection.AddAction(new ActionNode("view", Resources.isView, "查看", OnActionReviewClick) { IsShow = false })
              .AddAction(new ActionNode("increase", Resources.isRefresh, "资金入账", OnActionIncreaseClick))
               .AddAction(new ActionNode("refresh", Resources.isRefresh, "刷新", OnActionRefreshClick));
        }

        public void Active(ViewActiveArgs args)
        {
            _promise
                .Then(() =>
                {
                    this.BeginInvoke(new Action(() =>
                    {
                        this.ctlLookUpCapitalChangedType.BindIntCodeTextItem(Domain.Constant.CapitalChangedType.EnumerateIncrease(),
                            Domain.Constant.CapitalChangedType.AllItem, true);
                        _promise.Resolve();
                    }));
                })
                .Then(RefreshData)
                .Run(null);
        }

        public void Resume(ViewResumeArgs args)
        {
            if (args.ClosedArgs.Result)
            {
                RefreshData();
            }
        }

        public ViewHeader GetHeader()
        {
            return new ViewHeader("资金入账列表");
        }
        #endregion

        #region Method
        /// <summary>
        /// 获取选择的单据类型
        /// </summary>
        /// <returns></returns>
        private int GetCapitalChangedType()
        {
            Domain.IntCodeTextItem item = this.ctlLookUpCapitalChangedType.GetSelectedValue<Domain.IntCodeTextItem>();
            if (item == null)
            {
                return Domain.Constant.CapitalChangedType.All;
            }
            return item.Code;
        }

        private int GetPageLimit()
        {
            return 25;
        }
        /// <summary>
        /// 数据绑定
        /// </summary>
        private void RefreshData()
        {
            DateTime? beginTime = null, endTime = null;
            if (!string.IsNullOrEmpty(this.txtBeginDate.Text.Trim()))
            {
                beginTime = Convert.ToDateTime(this.txtBeginDate.Text.Trim());
            }
            if (!string.IsNullOrEmpty(this.txtEndDate.Text.Trim()))
            {
                endTime = Convert.ToDateTime(this.txtEndDate.Text.Trim());
            }
            this.ShowMask("请稍等...");
            UIContext.Current.InvokeAsync(() =>
            {
                PagedResult<WebRef.VCapitalIncreaseBill> result = new Service.QueryService().QueryCapitalIncreaseBillPagedList(
                    ModuleContext.Current.BusinessId.Value,
                    ModuleContext.Current.StationId.Value,
                    beginTime,
                    endTime,
                    GetCapitalChangedType(),
                    GetPageLimit() * _pageIndex,
                    GetPageLimit()
                );
                this.BeginInvoke(new Action(() =>
                {
                    if (result.IsSuccess)
                    {
                        this.GCData.Paginate<WebRef.VCapitalIncreaseBill>(result, out _pageIndex, out _pageCount);
                        _promise.Resolve();
                    }
                    else
                    {
                        UIContext.Current.ViewManager.ShowError(result.Message);
                        _promise.Reject();
                    }
                    this.HideMask();
                }));
            });
        }
        #endregion

        #region Action
        /// <summary>
        /// 查看
        /// </summary>
        private void OnActionReviewClick(object obj1, object obj2)
        {

        }
        /// <summary>
        /// 入账
        /// </summary>
        /// <param name="obj1"></param>
        /// <param name="obj2"></param>
        private void OnActionIncreaseClick(object obj1, object obj2)
        {
            UIContext.Current.ViewManager.PopupView(new Create.CreateCapitalIncreaseView(), new PopupViewArgs("新建资金入账"));
        }
        /// <summary>
        /// 刷新
        /// </summary>
        /// <param name="obj1"></param>
        /// <param name="obj2"></param>
        private void OnActionRefreshClick(object obj1, object obj2)
        {
            _pageIndex = 0;
            RefreshData();
        }
        void EmbeddedNavigator_ButtonClick(object sender, DevExpress.XtraEditors.NavigatorButtonClickEventArgs e)
        {
            if (e.UpdatePageIndex(_pageCount, ref _pageIndex))
            {
                RefreshData();
            }
        }
        #endregion
    }
}
